#include <QDateTime>
#include <QDebug>

#include "autoreplyprint.h"

#include "print_cmd.h"

void *prt_hndl = nullptr;

#define LABEL_company				QObject::tr("Company")
#define LABEL_report_type			QObject::tr("*** Report Type ***")
#define LABEL_client_name			QObject::tr("Client Name: ")
#define LABEL_terminal_name			QObject::tr("Terminal Name: ")
#define LABEL_terminal_id			QObject::tr("Terminal ID: ")
#define LABEL_user_name				QObject::tr("User Name: ")
#define LABEL_account_id			QObject::tr("User Account: ")
#define LABEL_date					QObject::tr("Date: ")

#define STR_client_name				QObject::tr("client name")
#define STR_terminal_name			QObject::tr("terminal name")
#define STR_terminal_id				QObject::tr("terminal ID")
#define STR_user_name				QObject::tr("user name")
#define STR_account_id				QObject::tr("user account")

int prtr_init()
{
	char buff[0x1000];

	if (CP_Port_EnumUsb(buff, sizeof(buff), NULL)) {
		char *pdev = buff;
		while (strlen(pdev)) {
			qInfo() << pdev;
			prt_hndl = CP_Port_OpenUsb(pdev, 1);
			if (prt_hndl)
				break;
			pdev += strlen(pdev) + 1;
		}
	}

	if (prt_hndl)
		return 0;

	return -1;
}

int prtr_deinit()
{
	if (prt_hndl)
		CP_Port_Close(prt_hndl);
	prt_hndl = nullptr;

	return 0;
}

int prtr_command(uint32_t cmd)
{
	Q_UNUSED(cmd);
	char data[0x1000], *p;
	QString date;

	if (prt_hndl == nullptr)
		prtr_init();
	if (!CP_Port_IsConnectionValid(prt_hndl)) {
		prtr_deinit();
		prtr_init();
	}
	if (prt_hndl == nullptr || !CP_Port_IsConnectionValid(prt_hndl))
		return -1;

	date = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");

	CP_Pos_ResetPrinter(prt_hndl);
    CP_Pos_SetMultiByteEncoding(prt_hndl, CP_MultiByteEncoding_GBK);

    CP_Pos_SetAlignment(prt_hndl, CP_Pos_Alignment_HCenter);
	p = data;
    strcpy(p, LABEL_company.toLocal8Bit()); p += strlen(p); strcpy(p, "\n\n"); p += strlen(p);
	strcpy(p, LABEL_report_type.toLocal8Bit()); p += strlen(p); strcpy(p, "\n\n"); p += strlen(p);
    CP_Port_Write(prt_hndl, (const unsigned char *) data, (unsigned int) strlen(data), 1000);
    CP_Pos_SetAlignment(prt_hndl, CP_Pos_Alignment_Left);

	p = data;
    strcpy(p, LABEL_client_name.toLocal8Bit()); p += strlen(p);
    strcpy(p, STR_client_name.toLocal8Bit()); p += strlen(p); strcpy(p, "\n"); p += strlen(p);
    strcpy(p, LABEL_terminal_name.toLocal8Bit()); p += strlen(p);
    strcpy(p, STR_terminal_name.toLocal8Bit()); p += strlen(p); strcpy(p, "\n"); p += strlen(p);
    strcpy(p, LABEL_terminal_id.toLocal8Bit()); p += strlen(p);
    strcpy(p, STR_terminal_id.toLocal8Bit()); p += strlen(p); strcpy(p, "\n"); p += strlen(p);
    strcpy(p, LABEL_user_name.toLocal8Bit()); p += strlen(p);
    strcpy(p, STR_user_name.toLocal8Bit()); p += strlen(p); strcpy(p, "\n"); p += strlen(p);
    strcpy(p, LABEL_account_id.toLocal8Bit()); p += strlen(p);
    strcpy(p, STR_account_id.toLocal8Bit()); p += strlen(p); strcpy(p, "\n"); p += strlen(p);
    strcpy(p, LABEL_date.toLocal8Bit()); p += strlen(p);
    strcpy(p, date.toLatin1()); p += strlen(p); strcpy(p, "\n\n\n"); p += strlen(p);

    CP_Port_Write(prt_hndl, (const unsigned char *) data, (unsigned int) strlen(data), 1000);

	return 0;
}

/*
AUTOREPLYPRINT_API unsigned int CP_Port_EnumUsb(char *pBuf, unsigned int cbBuf, unsigned int *pcbNeeded);
AUTOREPLYPRINT_API void *CP_Port_OpenUsb(const char *name, int autoreplymode);
AUTOREPLYPRINT_API int CP_Port_Close(void *handle);
AUTOREPLYPRINT_API int CP_Port_IsConnectionValid(void *handle);
AUTOREPLYPRINT_API int CP_Port_IsOpened(void *handle);
*/
